<?php

namespace App\Http\Controllers\v1\users;

use App\Http\Controllers\Controller;
use App\services\v1\SignRecordServices;
use Illuminate\Http\JsonResponse;

/**
 * 用户签到记录
 */

class SignRecordController extends Controller
{
    /**
     * @var SignRecordServices
     */
    private $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new SignRecordServices();
    }

    /**
     * 签到
     * @return false|JsonResponse|string
     * @throws \RedisException
     */
    public function getSign()
    {
        $servicesRes = $this->service->setSign();
        if ($servicesRes['code'] != 200) {
            return $this->error($servicesRes['code'],$servicesRes['data'],$servicesRes['msg']);
        }
        return $this->success();
    }

    /**
     * 当月签到数据
     * @return false|JsonResponse|string
     * @throws \RedisException
     */
    public function signRecord()
    {
        $servicesRes = $this->service->getSignDays();
        if ($servicesRes['code'] !== 200) {
            return $this->error($servicesRes['code'],$servicesRes['data'],$servicesRes['msg']);
        }
        return $this->success($servicesRes['data'],$servicesRes['msg']);
    }

    /**
     * 补签
     * @return false|JsonResponse|string
     */
    public function counterSign()
    {
        $this->validate(request(),[
            'counterDay' => 'required|date',
        ],[],[
            'counterDay' => '补签日期',
        ]);
        $counterDay  = date(date('Ymd', strtotime(request('counterDay'))));
        $serviceRes = $this->service->setCounterSign($counterDay);
        if ($serviceRes['code'] != 200) {
            return $this->error($serviceRes['code'], $serviceRes['data'], $serviceRes['msg']);
        }
        return $this->success();
    }

}